using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
using Identity.Application.Contracts.Requests.RolePermission;
using Identity.Application.RolePermissions.Commands;
using Identity.HttpAPI.Permission;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Identity.HttpAPI.Controllers
{
    [Authorize]
    [ApiController]
    [Route("api")]
    public class RolePermissionController : ControllerBase
    {

        private readonly IMediator _mediator;

        public RolePermissionController(IMediator mediator)
        {
            _mediator = mediator;
        }


        [HttpPost("roles/{roleId}/permissions")]
        [Authorize(Roles ="Admin")]
        [Permission("role:permission:assign" , "赋予角色权限")]
        public async Task<IActionResult> AssignPermissionToRole(Guid roleId, [FromBody] AssignPermissionToRoleRequest req)
        {
            var command = new AssignPermissionToRoleCommand(roleId, req.permissionIds);
            var result = await _mediator.Send(command);
            return Ok(result);

        }


        [HttpDelete("roles/{roleId}/permissions")]
        [Authorize(Policy = "permission:delete")]
        [Permission("role:permission:delete" , "移除角色权限")]

        public async Task<IActionResult> RemovePermissionFromRole(Guid roleId, [FromBody] RemovePermissionFromRoleRequest req)
        {
            var command = new RemovePermissionFromRoleCommand(roleId, req.permissionIds);
            var result = await _mediator.Send(command);
            return Ok(result);
        }
    }
}